/*
 *  Copright: 2008 Philipp Leitner, Distributed Systems Group,
 *            TU Wien, leitner@infosys.tuwien.ac.at
 *
 *  This file is part of DAIOS.
 *
 *  DAIOS is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation.
 * 
 *  DAIOS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with DAIOS.  If not, see
 *  <http://www.gnu.org/licenses/>.
 * 
 */

package at.ac.tuwien.infosys.dsg.daios.exceptions;

/**
 * Thrown to indicate that a Daios message field has a different
 * type than which the caller seemed to expect. For instance
 * a TypeErrorException might be thrown if the caller wants to
 * access a message field as "String", altough the field is defined
 * as type "Integer" in the message.
 * 
 *  <p>
 *  
 *  If you do not know the type of a field in the message in advance
 *  it is always safe to use the standard "get" method of Daios
 *  messages. They do not imply any constraint on the type of the
 *  field, and return a java.lang.Object. You can then use the
 *  Java introspection features to figure out the actual type if
 *  necessary.
 *  
 *  <p>
 *  
 *  If you <i>do</i> know the type of the message field you want to
 *  access it is easier and better (since type-safe) to use the
 *  specific getter, e.g. "getInt".
 * 
 * @author philipp
 *
 */
public class TypeErrorException extends Exception {
	
	private static final long serialVersionUID = 8810037086220265038L;

	private String key = null;
	private Class wanted = null;
	private Class actual = null;
	
	public TypeErrorException(String key, Class wanted, Class actual) {
		this.key = key;
		this.wanted = wanted;
		this.actual = actual;
	}
	
	public String getMessage() {
		if(wanted != null && actual != null)
			return "Type Error: Expecting "+wanted.getCanonicalName()+" for key "+
				key+", but found "+actual.getCanonicalName();
		else
			return "Type Error: for key "+key;
	}
}
